package com.jevons.geo_lib.geojson.adapter;

import androidx.annotation.Keep;

import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import com.mapbox.geojson.Point;
import com.mapbox.geojson.exception.GeoJsonException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Copyright 2020 JxGIS
 *
 * @file ListOfListOfPointCoordinatesTypeAdapter
 * @auth linaisheng
 * Created on 2022/7/13.
 * Description：
 */
@Keep
class ListOfListOfPointCoordinatesTypeAdapter
        extends BaseCoordinatesTypeAdapter<List<List<Point>>> {

    @Override
    public void write(JsonWriter out, List<List<Point>> points) throws IOException {

        if (points == null) {
            out.nullValue();
            return;
        }

        out.beginArray();

        for (List<Point> listOfPoints : points) {

            out.beginArray();

            for (Point point : listOfPoints) {
                writePoint(out, point);
            }
            out.endArray();
        }

        out.endArray();
    }

    @Override
    public List<List<Point>> read(JsonReader in) throws IOException {

        if (in.peek() == JsonToken.NULL) {
            throw new NullPointerException();
        }

        if (in.peek() == JsonToken.BEGIN_ARRAY) {

            in.beginArray();
            List<List<Point>> points = new ArrayList<>();

            while (in.peek() == JsonToken.BEGIN_ARRAY) {

                in.beginArray();
                List<Point> listOfPoints = new ArrayList<>();

                while (in.peek() == JsonToken.BEGIN_ARRAY) {
                    listOfPoints.add(readPoint(in));
                }
                in.endArray();
                points.add(listOfPoints);
            }
            in.endArray();

            return points;
        }

        throw new GeoJsonException("coordinates should be array of array of array of double");
    }
}
